<template>
  <el-dialog
    width="500px"
    title="修改地址"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
  >
    <el-form
      ref="ruleForm"
      class="ruleForm"
      :model="form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item
        label="姓名"
        prop="realname"
      >
        <el-input
          v-model="form.realname"
          clearable
          maxlength="10"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item
        label="联系电话"
        prop="mobile"
      >
        <el-input
          v-model="form.mobile"
          clearable
          maxlength="11"
          placeholder="请输入联系电话"
          @input="handleMobileChange"
        />
      </el-form-item>
      <el-form-item
        label="地区"
        prop="province"
      >
        <el-cascader
          v-model="form.province"
          clearable
          filterable
          placeholder="请选择地区"
          :options="areaList"
          :props="optionProps"
          separator="-"
          @change="form.address=''"
        />
      </el-form-item>
      <el-form-item
        label="详细地址"
        prop="address"
      >
        <el-input
          v-model="form.address"
          type="textarea"
          :rows="4"
          placeholder="请输入详细地址"
          :maxlength="200"
        />
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
    >
      <el-button
        size="small"
        @click="dialogVisible = false"
      >取 消</el-button>
      <el-button
        size="small"
        type="primary"
        @click="submit"
      >确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setAddress } from '@/api/order'
import { getRegion } from '@/api/index'
import { checkMobile, checkString } from '@/utils/validate'
export default {
  props: ['editItem'],
  data() {
    const validateMobile = (rule, value, callback) => {
      if (!checkMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    const validateString = (rule, value, callback) => {
      if (!checkString(value)) {
        callback(new Error('不能包含特殊字符'))
      } else {
        callback()
      }
    }
    return {
      form: {
        orderId: '',
        realname: '',
        mobile: '',
        address: '',
        prov: '',
        city: '',
        area: '',
        province: ''
      },
      rules: {
        realname: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { validator: validateString, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请选择地区', trigger: 'change' }
        ],
        address: [
          { required: true, message: '详细地址不能为空', trigger: 'blur' }
          // { validator: validateString, trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      optionProps: {
        value: 'name',
        label: 'name',
        expandTrigger: 'hover',
        children: 'sub'
      },
      areaList: []
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.init()
      }
    }
  },
  methods: {
    handleMobileChange() {
      this.form.mobile = this.form.mobile.replace(/[^\d]/g, '')
    },
    submit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          const params = { ...this.form }
          params.prov = params.province[0]
          params.city = params.province[1]
          params.area = params.province[2]
          delete params.province
          setAddress(params).then(res => {
            this.dialogVisible = false
            this.$emit('getList', true)
          })
        } else {
          this.$message({
            message: '请检查表单错误',
            type: 'error'
          })
          return false
        }
      })
    },
    init() {
      getRegion().then(res => {
        this.areaList = res ? res[0].sub : []
        for (const key in this.form) {
          this.form[key] = this.editItem[key] || ''
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
